﻿using System;
using System.Windows.Forms;
using Business.Forms;

namespace Business.Controls
{
    public class CommandNovo : CommandBase
    {
        public CommandNovo()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CommandDelete
            // 
            this.Text = "Novo";
            Atalho = Keys.F7;
            this.Click += new System.EventHandler(this.CommandDelete_Click);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CommandInsert_KeyDown);
            this.ResumeLayout(false);

        }

        private void CommandInsert_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyData == Atalho)
            {
                Novo();
            }
        }

        private void Novo()
        {
            try
            {
                Form.FormState = FormState.Inserting;
                Form.Form.DialogResult = DialogResult.None;
                Form.New();
            }
            catch (Exception ex)
            {
                ExibeErro("Erro ao criar item.\n");
            }
        }

        private void CommandDelete_Click(object sender, System.EventArgs e)
        {
            Novo();
        }

        public override void Actived()
        {
            if (Form == null)
            {
                return;
            }
            try
            {
                var prem = Seguranca.SegurancaUtils.GetPermissaoForm(Form.Form.Name);
                if (Form != null && Form.Permissao != null && prem.PermissaoInsert)
                {
                    Enabled = true;
                }
                else
                {
                    Enabled = false;
                }
            }
            catch (Exception)
            {
                Enabled = false;
            }
            
        }
    }
}
